#include <iostream>
#include <algorithm>
#include <cstring>


using namespace std;
const int N = 110,  INF = 0x3f3f3f3f;

int n;
int g[N][N];
int dist[N];
bool st[N];

int prime(){
    int res = 0;
    for(int i = 0; i < n; i ++){
        int t = -1;
        for(int j = 1; j <= n; j ++){
            if(!st[j] && (t == -1 || dist[t] > dist[j])){
                t = j;
            }
        }

        if(i && dist[t] == INF) return -1;
        if(i) res += dist[t];
        st[t] = true;
        
        for(int j = 1; j <= n; j ++){
            dist[j] = min(dist[j], g[t][j]);
        }
    }

    return res;
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    memset(dist, 0x3f, sizeof dist);

    scanf("%d", &n);
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            scanf("%d", &g[i][j]);
        }
    }

    int t = prime();


    printf("%d\n", t);

    return 0;
}